<template>
  <div>
    <el-dialog v-if="enable" :visible.sync="enable" title="文件下载" width="30%">
      <div style="margin-bottom:1em;">
        <div>远程文件：{{remoteFilePath}}</div>
        <div>存储路径：{{localFilePath}}</div>
      </div>
      <el-progress :text-inside="true" :stroke-width="26" :percentage="progress" :status="status"></el-progress>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="'success' != status" @click="actionCancel">取 消</el-button>
        <el-button type="primary" v-if="'success' == status" @click="actionClose">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
const fs = require("fs");
const {dialog} = require('@electron/remote')
export default {
    name : 'DialogFileDownload',
    data() {
        return {
            enable : false,
            localFilePath : null,
            remoteFilePath : null,
            fileSize : 0,
            downloadedSize : 0,
            status : 'success',
            readStream : null,
            writeStream : null,
        };
    },
    computed : {
        // 进度百分比
        progress() {
            if ( 0 == this.fileSize ) {
                return 0;
            }
            let val = (this.downloadedSize / this.fileSize * 100).toFixed(2);
            return parseFloat(val);
        }
    },
    methods : {
        /**
         * 下载文件
         * @param {Object} file
         */
        start( path, file ) {
            let folder = dialog.showOpenDialogSync({ properties: ['openDirectory'] });
            if ( undefined == folder ) {
                return;
            }
            folder = folder[0];
            this.localFilePath = folder + '/' + file.filename;

            let filepath = path.slice();
            filepath.push(file.filename);
            this.remoteFilePath = '/' + filepath.join('/');
            this.fileSize = file.attrs.size;

            this.download();
            this.status = null;
            this.enable = true;
        },

        // 下载文件
        async download() {
            this.writeStream = fs.createWriteStream(this.localFilePath);

            let $this = this;
            let sftp = await this.$store.getters.serverCurrent.getSftp();
            this.readStream = sftp.createReadStream(this.remoteFilePath);
            this.readStream.on('data',function (chunk) {
                console.log('data');
                $this.downloadedSize += chunk.length;
                $this.writeStream.write(chunk);
            });
            this.readStream.on('end',function (chunk) {
                console.log('end');
                $this.status = 'success';
                $this.writeStream.end();
            });
            this.readStream.on('error',function (err) {
               console.log("error");
            });
        },

        // 关闭弹框
        actionClose() {
            this.enable = false;
        },

        // 取消下载
        actionCancel() {
            this.writeStream.end();
            this.readStream.close();
            this.enable = false;
        },
    }
}
</script>